﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using QLTV_BLL;
using QLTV_Public;
using System.Threading;
namespace QLTV_GiaoDien
{
    public partial class frm_TaiLieu : DevExpress.XtraEditors.XtraForm
    {
        public frm_TaiLieu()
        {
            InitializeComponent();
        }
        private int _flag;

        public int Flag
        {
            get { return _flag; }
            set { _flag = value; }
        }
        private static int _maTaiLieu;

        public int MaTaiLieu
        {
            get { return _maTaiLieu; }
            set { _maTaiLieu = value; }
        }
        private static string _TenTaiLieu;

        public string TenTaiLieu
        {
            get { return _TenTaiLieu; }
            set { _TenTaiLieu = value; }
        }
        private static string _MaTheLoai;

        public string MaTheLoai
        {
            get { return _MaTheLoai; }
            set { _MaTheLoai = value; }
        }
        private static string _TacGia;

        public string TacGia
        {
            get { return _TacGia; }
            set { _TacGia = value; }
        }
        private static string _MaNhaXuatBan;

        public string MaNhaXuatBan
        {
            get { return _MaNhaXuatBan; }
            set { _MaNhaXuatBan = value; }
        }
        private static int _NamXuatBan;

        public int NamXuatBan
        {
            get { return _NamXuatBan; }
            set { _NamXuatBan = value; }
        }
        private static string _NoiDung;

        public  string NoiDung
        {
            get { return _NoiDung; }
            set { _NoiDung = value; }
        }
        private static string _NgonNgu;

        public string NgonNgu
        {
            get { return _NgonNgu; }
            set { _NgonNgu = value; }
        }
        private static int _SoTrang;

        public  int SoTrang
        {
            get { return _SoTrang; }
            set { _SoTrang = value; }
        }
        private static int _LanTaiBan;

        public int LanTaiBan
        {
            get { return _LanTaiBan; }
            set { _LanTaiBan = value; }
        }
        private static string _GiaBia;

        public string GiaBia
        {
            get { return _GiaBia; }
            set { _GiaBia = value; }
        }
        private static int _SoLuong;

        public int SoLuong
        {
            get { return _SoLuong; }
            set { _SoLuong = value; }
        }

        private static string _TinhTrang;

        public string TinhTrang
        {
            get { return _TinhTrang; }
            set { _TinhTrang = value; }
        }

        private static string _MaViTri;

        public string MaViTri
        {
            get { return _MaViTri; }
            set { _MaViTri = value; }
        }
        private static string _NgayCapNhat;

        public string NgayCapNhat
        {
            get { return _NgayCapNhat; }
            set { _NgayCapNhat = value; }
        }
        public void LoadForm()
        {
            TaiLieu_Public tailieu = new TaiLieu_Public();
            if (Flag == 0)
            {
                this.Text = "Thêm Tài Liệu";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
                bbi_Dau.Enabled = false;
                bbi_Sau.Enabled = false;
                bbi_Tiep.Enabled = false;
                bbi_Cuoi.Enabled = false;
            }
            if (Flag == 1)
            {
                this.Text = "Sửa Tài Liệu";
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
                getData();
            }
            if (Flag == 2)
            {
                this.Text = "Xem Tài Liệu";
                bbi_Luu.Enabled = false;
                bbi_Them.Enabled = false;
                bbi_Sua.Enabled = false;
                EnabledText(this);
                cob_TheLoai.Enabled = false;
                cob_NhaXuatBan.Enabled = false;
                cob_ViTri.Enabled = false;
                cob_TenNgonNgu.Enabled = false;
                getData();
            }
        }
        public void getData()
        {
            txt_MaTaiLieu.Text = MaTaiLieu.ToString();
            txt_TenTaiLieu.Text = TenTaiLieu;
            txt_TacGia.Text = TacGia;
            txt_NamXuatBan.Text = NamXuatBan.ToString();
            txt_NoiDung.Text = NoiDung;
            cob_TenNgonNgu.Text = NgonNgu;
            txt_SoTrang.Text = SoTrang.ToString();
            txt_LanTaiBan.Text = LanTaiBan.ToString();
            txt_GiaBia.Text = GiaBia;
            txt_SoLuong.Text = SoLuong.ToString();
            //txt_TinhTrang.Text = TinhTrang;
            //cob_ViTri.Text = MaViTri;
            dateT_NgayCapNhat.Text = NgayCapNhat;
            DataTable dt = new DataTable();

            //lấy dữ liệu đổ vào trường thể loại

            string sql_TheLoai = "select * from tbl_TheLoai,tbl_TaiLieu where tbl_TheLoai.MaTheLoai=tbl_TaiLieu.MaTheLoai and MaTaiLieu='"+txt_MaTaiLieu.Text+"'";
            dt=tailieu_bll.taoBang(sql_TheLoai);
            cob_MaTheLoai.DataSource = dt;
            cob_MaTheLoai.DisplayMember = "MaTheLoai";
            cob_TheLoai.DataSource = dt;
            cob_TheLoai.DisplayMember = "TenTheLoai";

            //lấy dữ liệu đổ vào trường nhà xuất bản

            string sql_NhaXuatBan = "select * from tbl_NhaXuatBan,tbl_TaiLieu where tbl_NhaXuatBan.MaNhaXuatBan=tbl_TaiLieu.MaNhaXuatBan and MaTaiLieu='" + txt_MaTaiLieu.Text + "'";
            dt = tailieu_bll.taoBang(sql_NhaXuatBan);
            cob_MaNXB.DataSource = dt;
            cob_MaNXB.DisplayMember = "MaNhaXuatBan";
            cob_NhaXuatBan.DataSource = dt;
            cob_NhaXuatBan.DisplayMember = "TenNhaXuatBan";

            //lấy dữ liệu đổ vào trường ngôn ngữ
            string sql_NgonNgu = "select * from tbl_NgonNgu,tbl_TaiLieu where tbl_NgonNgu.MaNgonNgu=tbl_TaiLieu.MaNgonNgu and tbl_TaiLieu.MaTaiLieu='"+txt_MaTaiLieu.Text+"'";
            dt = tailieu_bll.taoBang(sql_NgonNgu);
            cob_MaNgonNgu.DataSource = dt;
            cob_MaNgonNgu.DisplayMember = "MaNgonNgu";
            cob_TenNgonNgu.DataSource = dt;
            cob_TenNgonNgu.DisplayMember = "TenNgonNgu";

            //lấy dữ liệu đổ vào trường vị trí lưu trữ

            string sql_ViTri = "select * from tbl_ViTriLuuTru,tbl_TaiLieu where tbl_ViTriLuuTru.MaViTri=tbl_TaiLieu.MaViTri and MaTaiLieu='" + txt_MaTaiLieu.Text + "'";
            dt = tailieu_bll.taoBang(sql_ViTri);
            cob_MaViTri.DataSource = dt;
            cob_MaViTri.DisplayMember = "MaViTri";
            cob_ViTri.DataSource = dt;
            cob_ViTri.DisplayMember = "TenViTri";
        }
        public void EnabledText(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                //ctr.Text = string.Empty;
                ctr.Enabled=false;
            }
            foreach (Control i in ctr.Controls)
            {
                EnabledText(i);
            }
        }
        public void EnabledText_True(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                //ctr.Text = string.Empty;
                ctr.Enabled = true;
            }
            foreach (Control i in ctr.Controls)
            {
                EnabledText_True(i);
            }
        }
        public void Huy_Text(Control ctr)
        {
            if (ctr is TextBox || ctr is TextEdit)
            {
                //ctr.Text = string.Empty;
                ctr.Text = string.Empty;
            }
            foreach (Control i in ctr.Controls)
            {
                Huy_Text(i);
            }
        }
        private void frm_Them_TaiLieu_Load(object sender, EventArgs e)
        {
            LoadForm();
            cob_MaTheLoai.Hide();
            cob_MaNXB.Hide();
            cob_MaViTri.Hide();
            cob_MaNgonNgu.Hide();
        }
        public void getMaTheLoai(int ma)
        {
            cob_MaTheLoai.Text = ma.ToString();
        }
        public void getMaNXB(int ma)
        {
            cob_MaNXB.Text = ma.ToString();
        }
        public void getMaViTri(int ma)
        {
            cob_MaViTri.Text = ma.ToString();
        }
        public void GetMaNgonNgu(int ma)
        {
            cob_MaNgonNgu.Text = ma.ToString();
        }
        private void btn_TheLoai_Click(object sender, EventArgs e)
        {
            frm_TaiLieu_TheLoai f = new frm_TaiLieu_TheLoai();
            f.MyGetData = new frm_TaiLieu_TheLoai.GetInt(getMaTheLoai);
            f.ShowDialog();
        }

        private void btn_NhaXuatBan_Click(object sender, EventArgs e)
        {
            frm_TaiLieu_NhaXuatBan f = new frm_TaiLieu_NhaXuatBan();
            f.MyData = new frm_TaiLieu_NhaXuatBan.GetInt(getMaNXB);
            f.ShowDialog();
        }
        private void btn_ViTri_Click(object sender, EventArgs e)
        {
            frm_TaiLieu_ViTri f = new frm_TaiLieu_ViTri();
            f.MyGetData = new frm_TaiLieu_ViTri.GetInt(getMaViTri);
            f.ShowDialog();
        }
        public delegate void GetInt(int load);
        public GetInt MyGetLoad;
        TaiLieu_BLL tailieu_bll = new TaiLieu_BLL();

        NhatKy_BLL nhatKy_bll = new NhatKy_BLL();
        NhatKy_Public nk = new NhatKy_Public();
        TrungGian tg = new TrungGian();
        private void bbi_Luu_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
             TaiLieu_Public tl = new TaiLieu_Public();
            if (Flag == 0)
            {
                if (txt_TenTaiLieu.Text != "" && cob_TheLoai.Text != "" && cob_NhaXuatBan.Text != "" && cob_TenNgonNgu.Text != "" && cob_ViTri.Text != "" && txt_SoLuong.Text != "")
                {
                    //them
                    tl.TenTaiLieu = txt_TenTaiLieu.Text;
                    tl.TacGia = txt_TacGia.Text;
                    tl.MaTheLoai = int.Parse(cob_MaTheLoai.Text);
                    tl.MaNhaXuatBan = int.Parse(cob_MaNXB.Text);
                    tl.NamXuatBan = int.Parse("0" + txt_NamXuatBan.Text);
                    tl.NoiDung = txt_NoiDung.Text;
                    tl.MaNgonNgu = int.Parse(cob_MaNgonNgu.Text);
                    tl.SoTrang = int.Parse("0" + txt_SoTrang.Text);
                    tl.LanTaiBan = int.Parse("0" + txt_LanTaiBan.Text);
                    tl.GiaBia = float.Parse("0" + txt_GiaBia.Text);
                    tl.SoLuong = int.Parse("0" + txt_SoLuong.Text);
                    //tl.TinhTrang = txt_TinhTrang.Text;
                    tl.MaViTri = int.Parse(cob_MaViTri.Text);
                    tl.NgayCapNhat = Convert.ToDateTime(dateT_NgayCapNhat.Text);
                    tailieu_bll.insert_taiLieu(tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl);
                    //
                    string d = DateTime.Now.ToString();
                    nk.MaNV = tg.MaNV;
                    nk.NgayGio = d;
                    nk.HanhDong = "Thêm";
                    nk.MoTa = "Thêm Tài liệu: '" + txt_TenTaiLieu.Text + "'";
                    nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                    //
                    XtraMessageBox.Show("Thêm mới thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                    XtraMessageBox.Show("Bạn chưa nhập đủ thông tin!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            if (Flag == 1)
            {
                //sua
                if (txt_MaTaiLieu.Text != "" && txt_TenTaiLieu.Text != "" && cob_TheLoai.Text != "" && cob_NhaXuatBan.Text != "" && cob_TenNgonNgu.Text != "" && cob_ViTri.Text != "" && txt_SoLuong.Text != "")
                {
                    tl.MaTaiLieu = int.Parse(txt_MaTaiLieu.Text);
                    tl.TenTaiLieu = txt_TenTaiLieu.Text;
                    tl.TacGia = txt_TacGia.Text;
                    tl.MaTheLoai = int.Parse(cob_MaTheLoai.Text);
                    tl.MaNhaXuatBan = int.Parse(cob_MaNXB.Text);
                    tl.NamXuatBan = int.Parse("0" + txt_NamXuatBan.Text);
                    tl.NoiDung = txt_NoiDung.Text;
                    tl.MaNgonNgu = int.Parse(cob_MaNgonNgu.Text);
                    tl.SoTrang = int.Parse("0" + txt_SoTrang.Text);
                    tl.LanTaiBan = int.Parse("0" + txt_LanTaiBan.Text);
                    tl.GiaBia = float.Parse("0" + txt_GiaBia.Text);
                    tl.SoLuong = int.Parse("0" + txt_SoLuong.Text);
                    //tl.TinhTrang = txt_TinhTrang.Text;
                    tl.MaViTri = int.Parse(cob_MaViTri.Text);
                    tl.NgayCapNhat = Convert.ToDateTime(dateT_NgayCapNhat.Text);
                    tailieu_bll.update_taiLieu(tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl);
                    //
                    string d = DateTime.Now.ToString();
                    nk.MaNV = tg.MaNV;
                    nk.NgayGio = d;
                    nk.HanhDong = "Sửa";
                    nk.MoTa = "Sửa thông tin Tài liệu: '" + txt_TenTaiLieu.Text + "'";
                    nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                    //
                    XtraMessageBox.Show("Sửa thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                XtraMessageBox.Show("Bạn chưa nhập đủ thông tin để sửa!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            if (Flag == 2)
            {
                //sửa khi xem
                if (txt_MaTaiLieu.Text != "" && txt_TenTaiLieu.Text != "" && cob_TheLoai.Text != "" && cob_NhaXuatBan.Text != "" && cob_TenNgonNgu.Text != "" && cob_ViTri.Text != "" && txt_SoLuong.Text != "")
                {
                    tl.MaTaiLieu = int.Parse(txt_MaTaiLieu.Text);
                    tl.TenTaiLieu = txt_TenTaiLieu.Text;
                    tl.TacGia = txt_TacGia.Text;
                    tl.MaTheLoai = int.Parse(cob_MaTheLoai.Text);
                    tl.MaNhaXuatBan = int.Parse(cob_MaNXB.Text);
                    tl.NamXuatBan = int.Parse("0" + txt_NamXuatBan.Text);
                    tl.NoiDung = txt_NoiDung.Text;
                    tl.MaNgonNgu = int.Parse(cob_MaNgonNgu.Text);
                    tl.SoTrang = int.Parse("0" + txt_SoTrang.Text);
                    tl.LanTaiBan = int.Parse("0" + txt_LanTaiBan.Text);
                    tl.GiaBia = float.Parse("0" + txt_GiaBia.Text);
                    tl.SoLuong = int.Parse("0" + txt_SoLuong.Text);
                    //tl.TinhTrang = txt_TinhTrang.Text;
                    tl.MaViTri = int.Parse(cob_MaViTri.Text);
                    tl.NgayCapNhat = Convert.ToDateTime(dateT_NgayCapNhat.Text);
                    tailieu_bll.update_taiLieu(tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl, tl);
                    //
                    string d = DateTime.Now.ToString();
                    nk.MaNV = tg.MaNV;
                    nk.NgayGio = d;
                    nk.HanhDong = "Sửa";
                    nk.MoTa = "Sửa thông tin Tài liệu: '" + txt_TenTaiLieu.Text + "'";
                    nhatKy_bll.insert_nhatKy(nk, nk, nk, nk);
                    //
                    XtraMessageBox.Show("Sửa mới thành công!", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                XtraMessageBox.Show("Bạn chưa nhập đủ dữ liệu!","Thông Báo",MessageBoxButtons.OK,MessageBoxIcon.Information);
            }
            MyGetLoad(1);
            Huy_Text(this);
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Luu.Enabled = false;
            bbi_Huy.Enabled = false;
        }

        private void bbi_Thoat_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            this.Close();
        }

        private void cob_TheLoai_Click(object sender, EventArgs e)
        {// chú ý để mã  trước xong mới đến tên
            DataTable dt = new DataTable();
            string sql = "select * from tbl_TheLoai";
            dt = tailieu_bll.taoBang(sql);
            cob_MaTheLoai.DataSource = dt;
            cob_MaTheLoai.DisplayMember = "MaTheLoai";
            //
            cob_TheLoai.DataSource = dt;
            cob_TheLoai.DisplayMember = "TenTheLoai";
        }

        private void cob_NhaXuatBan_Click(object sender, EventArgs e)
        {

                DataTable dt = new DataTable();
                string sql = "select * from tbl_NhaXuatBan";
                dt = tailieu_bll.taoBang(sql);
                cob_MaNXB.DataSource = dt;
                cob_MaNXB.DisplayMember = "MaNhaXuatBan";
                cob_NhaXuatBan.DataSource = dt;
                cob_NhaXuatBan.DisplayMember = "TenNhaXuatBan";

        }
        private void cob_TenNgonNgu_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            string sql_NgonNgu = "select * from tbl_NgonNgu";
            dt = tailieu_bll.taoBang(sql_NgonNgu);
            cob_MaNgonNgu.DataSource = dt;
            cob_MaNgonNgu.DisplayMember = "MaNgonNgu";
            cob_TenNgonNgu.DataSource = dt;
            cob_TenNgonNgu.DisplayMember = "TenNgonNgu";
        }
        private void cob_ViTri_Click(object sender, EventArgs e)
        {
            DataTable dt = new DataTable();
            string sql = "select * from tbl_ViTriLuuTru";
            dt = tailieu_bll.taoBang(sql); 
            cob_MaViTri.DataSource = dt;
            cob_MaViTri.DisplayMember = "MaViTri";
            cob_ViTri.DataSource = dt;
            cob_ViTri.DisplayMember = "TenViTri";

        }

        private void cob_MaNXB_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                string sql = "select * from tbl_NhaXuatBan where MaNhaXuatBan='" + cob_MaNXB.Text + "'";
                dt = tailieu_bll.taoBang(sql);
                cob_NhaXuatBan.DataSource = dt;
                cob_NhaXuatBan.DisplayMember = "TenNhaXuatBan";
            }
            catch (Exception)
            { }
        }
        

        private void cob_MaTheLoai_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                string sql = "select * from tbl_TheLoai where MaTheLoai='" + cob_MaTheLoai.Text + "'";
                dt = tailieu_bll.taoBang(sql);
                cob_TheLoai.DataSource = dt;
                cob_TheLoai.DisplayMember = "TenTheLoai";
            }
            catch (Exception)
            { }
        }

        private void cob_MaViTri_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                string sql = "select * from tbl_ViTriLuuTru where MaViTri='" + cob_MaViTri.Text + "'";
                dt = tailieu_bll.taoBang(sql);
                cob_ViTri.DataSource = dt;
                cob_ViTri.DisplayMember = "TenViTri";
            }
            catch (Exception)
            { }
        }

        private void btn_NgonNgu_Click(object sender, EventArgs e)
        {
            frm_TaiLieu_NgonNgu f = new frm_TaiLieu_NgonNgu();
            f.MaNgonNgu = new frm_TaiLieu_NgonNgu.GetInt(GetMaNgonNgu);
            f.ShowDialog();
        }

        private void cob_MaNgonNgu_TextChanged(object sender, EventArgs e)
        {
            try
            {
                DataTable dt = new DataTable();
                string sql = "select * from tbl_NgonNgu where MaNgonNgu='" + cob_MaNgonNgu.Text + "'";
                dt = tailieu_bll.taoBang(sql);
                cob_TenNgonNgu.DataSource = dt;
                cob_TenNgonNgu.DisplayMember = "TenNgonNgu";
            }
            catch (Exception)
            {
                
            }
        }

        private void txt_NamXuatBan_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
            {
                
                e.Handled = true;
            }
        }

        private void bbi_Huy_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag = 4;
            Huy_Text(this);
            bbi_Them.Enabled = true;
            bbi_Sua.Enabled = true;
            bbi_Luu.Enabled = false;
            bbi_Huy.Enabled = false;
            EnabledText_True(this);
        }

        private void bbi_Sua_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag = 1;
            txt_MaTaiLieu.Enabled = true;
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
            this.Text = "Sửa Tài Liệu";
            getData();
        }

        private void bbi_Them_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            Flag = 0;
            this.Text = "Thêm Mới Tài Liệu";
            txt_MaTaiLieu.Enabled = false;
            txt_MaTaiLieu.Text = "Mã tự sinh";
            bbi_Them.Enabled = false;
            bbi_Sua.Enabled = false;
            bbi_Luu.Enabled = true;
            bbi_Huy.Enabled = true;
        }

        private void cob_TheLoai_Leave(object sender, EventArgs e)
        {
            if (cob_TheLoai.Text == "")
                cob_TheLoai.Focus();
        }

        private void cob_NhaXuatBan_Leave(object sender, EventArgs e)
        {
            if (cob_NhaXuatBan.Text == "")
                cob_NhaXuatBan.Focus();
        }

        private void cob_TenNgonNgu_Leave(object sender, EventArgs e)
        {
            if (cob_TenNgonNgu.Text == "")
                cob_TenNgonNgu.Focus();
        }

        private void cob_ViTri_Leave(object sender, EventArgs e)
        {
            if (cob_ViTri.Text == "")
                cob_ViTri.Focus();
        }

        private void txt_SoTrang_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void txt_MaTaiLieu_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }

        private void txt_LanTaiBan_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (!Char.IsDigit(e.KeyChar) && !Char.IsControl(e.KeyChar))
                e.Handled = true;
        }



    }
}